//
//  chooseAgeVC.m
//  YiXiu
//
//  Created by ZHY on 16/7/14.
//  Copyright © 2016年 zzz. All rights reserved.
//


#import "chooseAgeVC.h"

@interface chooseAgeVC ()

@property (nonatomic, copy) NSString *nameLabel;

@property (nonatomic, assign) NSInteger age;

@property (nonatomic, strong) UIDatePicker *datePicker;

@property (nonatomic, strong) UILabel *RightAgeLab;

@property(nonatomic, weak) UIButton * rigth;




@end

@implementation chooseAgeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xefeff4);
    [self.view addSubview:self.datePicker];
    [self setUpAge];
    [self setUpRightBtn];
    
}



-(void)setUpAge{
    
    CGFloat ageViewW =KScreenWidth -20;
    
    UIView  * ageView = [[UIView alloc]initWithFrame:CGRectMake(10, 84, ageViewW, 40)];
    ageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:ageView];
    
    UILabel * leftAgeLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 60, 40)];
    [leftAgeLab setText:@"年龄"];
    [ageView addSubview:leftAgeLab];
    
    _RightAgeLab = [[UILabel alloc]initWithFrame:CGRectMake(ageViewW - 30-5, 0, 60, 40)];
    
    [ageView addSubview:_RightAgeLab];
    

    
    
}

-(UIDatePicker *)datePicker{
    
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc]init];
        
        CGFloat datePickerH = KScreenHeight/3;
        _datePicker.frame = CGRectMake(5, KScreenHeight - datePickerH, KScreenWidth-10, datePickerH);
        
        [self.view addSubview:_datePicker];
        
        [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
        
        //改变模型样式
        _datePicker.datePickerMode = UIDatePickerModeDate;
        
        //设置显示模式
        NSLocale * locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        _datePicker.locale = locale;
        

        
        //设置最小值
        
        
        //设置最大值
        NSDate * maxDate = [NSDate date];
        //设置属性
//        _datePicker.minimumDate = localDate;
        _datePicker.maximumDate = maxDate;
    }

    return _datePicker;
    
}


-(void)dateChange:(id)sender{

    [self caculate];
}


//计算事件差
-(void)caculate{


    NSDate * date = self.datePicker.date;
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    NSDateComponents * chooesdate = [calendar components:unitFlags fromDate:date];
    int nowYear  = [dateComponent year];
    int chooseYear = [chooesdate year];
    self.age = nowYear - chooseYear;
    NSString * ageStr = [NSString stringWithFormat:@"%ld岁",(long)self.age];
    [_RightAgeLab setText:ageStr];
    NSLog(@"%ld",(long)_age);
    
}


-(void)setUpRightBtn{
    
    UIButton * rightItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
    self.rigth = rightItem;

    
    [rightItem addTarget:self action:@selector(releaseInfo:) forControlEvents:UIControlEventTouchUpInside];
    [rightItem setTitle:@"提交" forState:UIControlStateNormal];
    [rightItem setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItem];
    
    
}


-(void)releaseInfo:(UIButton *)sender{
    
    //__weak 弱引用 避免循环引用
    __weak typeof (self) weakSelf = self;
    if (weakSelf.block) {
        weakSelf.block(weakSelf.RightAgeLab.text);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}






@end
